package com.mall.web.control.jiance;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.client.ps.PsService;
import com.mall.client.ps.PsVideoService;
import com.mall.common.util.PageUtil;
import com.mall.pojo.PsItem;
import com.mall.pojo.PsItemExample;
import com.mall.pojo.PsVideo;
import com.mall.pojo.PsVideoExample;
import com.mall.pojo.PsVideoExample.Criteria;
import com.mall.query.Page;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.StringUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/ps/video/")
public class PsVideoController {

	@Autowired
	private PsVideoService psVideoService;
	@Autowired
	private PsService psService;

	/**
	 * 视频首页
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return "ps/video/index";
	}
	
	@RequestMapping("kefu")
	public String indexDown(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return "ps/video/kefu";
	}
	
	/**
	 * 校验提取码
	 */
	@RequestMapping("checkCodeAction")
	public String checkCodeAction(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		//ResponseEntity<PsItem> responseEntity = new ResponseEntity<>();
		String code = HttpUtil.getStringValueFromRequest(request, "code");
		Integer kefu = HttpUtil.getIntegerValueFromRequest(request, "kefu");
		if (code == null) {
			return "redirect:/ps/video/error.htm";
		}
		
		PsItemExample example = new PsItemExample();
		com.mall.pojo.PsItemExample.Criteria  criteria = example.createCriteria();
		criteria.andVideoCodeEqualTo(code);
        List<PsItem> psItemList = this.psService.getPsItemListByExample(example);
        PsItem psItem = null;
		if (psItemList !=null && psItemList.size()>0) {
			 psItem = psItemList.get(0);
			if (kefu==0) {
			
				 if (psItem.getType()==0) {
					 return "redirect:/ps/video/psVideoCourseIndex.htm?psItemId=" + psItem.getId()+"&code="+code;
				}else if (psItem.getType()==1) {
					return "redirect:/ps/video/psLook.htm?psItemId=" + psItem.getId();
				} 
			}else if(kefu==1){
				return "redirect:/ps/video/kefuModify.htm?psItemId=" + psItem.getId();
			}
		} 
			
		
	
		return "redirect:/ps/video/error.htm";

	}
	@RequestMapping("kefuModify")
	public String kefuModify(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		Integer psItemId = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "psItemId", 0);
		if (psItemId<=0) {
			return "error";
		}
		PsItem psItem = this.psService.getPsItemById(psItemId);
		modelMap.put("psItem", psItem);
		return "ps/video/kefuModify";
		
	}
	
	@RequestMapping("kefuModifyAction")
	public String kefuModifyAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		Integer psItemId = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "psItemId", 0);
		if (psItemId<=0) {
			return "redirect:/ps/video/error.htm";
		}
		PsItem psItem = this.psService.getPsItemById(psItemId);
		String yunpanUrl = HttpUtil.getStringValueFromRequest(request, "yunpanUrl");
		String yunpanPass = HttpUtil.getStringValueFromRequest(request, "yunpanPass");
		String ruanjianUrl = HttpUtil.getStringValueFromRequest(request, "ruanjianUrl");
		String ruanjianPass = HttpUtil.getStringValueFromRequest(request, "ruanjianPass");
		if (StringUtils.isNotBlank(yunpanUrl)) {
			psItem.setYunpanUrl(yunpanUrl);	
		}
		if (StringUtils.isNotBlank(yunpanPass)) {
			psItem.setYunpanPass(yunpanPass);
		}
		psItem.setRuanjianUrl(ruanjianUrl);
		psItem.setRuanjianPass(ruanjianPass);
		Integer result = this.psService.modifyPsItem(psItem);
		if(result == null || result<=0){
			return "redirect:/ps/video/error.htm";
		}
		return "redirect:/ps/video/success.htm";
		
	}
	
	
	
	@RequestMapping("error")
	public String error(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		return "ps/video/error";
		
	}
	@RequestMapping("success")
	public String success(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		return "ps/video/success";
		
	}
	
	@RequestMapping("psVideoCourseIndex")
	public String psVideoCourseIndex(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		Integer psItemId = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "psItemId", 0);
		if (psItemId<=0) {
			return "error";
		}
		PsItem psItem = this.psService.getPsItemById(psItemId);
		PsVideoExample example = getPsVideoExampleBy(null, null, null,psItemId);
		
		int categoryCount = this.psVideoService.getVideoCategoryCountByPsVideoExample(example);
		List<PsVideo> psVideoCategoryList = this.psVideoService.getCategoryListByPsVideoExample(example);
		List<Integer> categoryList = new ArrayList<>();
		for (int i = 0; i < psVideoCategoryList.size(); i++) {
			int category = psVideoCategoryList.get(i).getCategory();
			categoryList.add(category);

		}
		String code = HttpUtil.getStringValueFromRequest(request, "code");
		modelMap.put("psVideoCategoryList", psVideoCategoryList);
	//	List<PsVideo> psVideoList = this.psVideoService.getPsVideoListByExample(example);
		modelMap.put("psItem", psItem);
		modelMap.put("categoryList", categoryList);
		modelMap.put("categoryCount", categoryCount);
		modelMap.put("code", code);
		return "ps/video/psVideoCourseIndex";
		
	}
	
	@RequestMapping("psLook")
	public String psDownload(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		Integer psItemId = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "psItemId", 0);
		if (psItemId<=0) {
			return "error";
		}
		PsItem psItem = this.psService.getPsItemById(psItemId);
		modelMap.put("psItem", psItem);
		return "ps/video/psDownload";
		
	}
	

	/**
	 * 校验提取码
	 */
	@RequestMapping("checkCode")
	@ResponseBody
	public ResponseEntity<String> checkCode(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		ResponseEntity<String> responseEntity = new ResponseEntity<>();
		String code = HttpUtil.getStringValueFromRequest(request, "code");
		Integer category = HttpUtil.getIntegerValueFromRequest(request, "category");
		if (code == null || category == null) {
			responseEntity.initErrorResponseEntity(null, "code is error");
			return responseEntity;
		}
		Page page = new Page();
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize", PageUtil.PAGE_SIZE_APP);
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		PsVideoExample example = getPsVideoExampleBy(page, category, code,null);
		List<PsVideo> psVideoList = this.psVideoService.getPsVideoListByExample(example);
		if (psVideoList.size() == 0 || psVideoList == null) {
			responseEntity.initErrorResponseEntity(null, "code is error");
			return responseEntity;
		}
		responseEntity.initSuccResponseEntity(code, "check success");
		return responseEntity;

	}

//	/**
//	 * 视频详情播放页面
//	 * @param request
//	 * @param response
//	 * @param modelMap
//	 * @return
//	 */
//	@RequestMapping("psVideoCourseIndex")
//	public String psVideoCourseDetail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
//		Integer courseId = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "courseId", 0);
//		if (courseId<=0) {
//			return "error";
//		}
//		PsItem psItem = this.psService.getPsItemById(courseId);
//		modelMap.put("psItem", psItem);
//		return "ps/video/psVideoCourseIndex";
//		
//	}

	/**
	 * 课程列表
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */

	@RequestMapping("psVideoList")
	public String psVideoList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		psVideoListMap(request, response, modelMap);
		return "ps/video/psVideoList";

	}

	/**
	 * 视频翻页
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("psVideoListMore")
	public String psVideoListMore(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		psVideoListMap(request, response, modelMap);
		return "ps/video/psVideoListMore";

	}

	/**
	 * 视频详情播放页面
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("psVideoDetail")
	public String psVideoDetail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long psVideoId = HttpUtil.getLongValueFromRequest(request, "psVideoId");
		PsVideo psVideo = this.psVideoService.getPsVideoById(psVideoId);
		modelMap.put("psVideo", psVideo);
		return "ps/video/psVideoDetail";

	}

	private ModelMap psVideoListMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String code = HttpUtil.getStringValueFromRequest(request, "code");

		Integer category = HttpUtil.getIntegerValueFromRequest(request, "category");
		Page page = new Page();
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize", PageUtil.PAGE_SIZE);
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		PsVideoExample example = getPsVideoExampleBy(page, category, code,null);
		List<PsVideo> psVideoList = this.psVideoService.getPsVideoListByExample(example);// 视频列表

		PsVideoExample exampleCount = getPsVideoExampleBy(null, category, code,null);
		Integer count = this.psVideoService.getCountByExample(exampleCount);
		PsItem psItem = null;
		if (psVideoList != null && psVideoList.size() > 0) {
			Integer psItemId = psVideoList.get(0).getPsItemId();
			if (psItemId != null && psItemId > 0) {
				psItem = this.psService.getPsItemById(psItemId);
			}
		}
		modelMap.put("psItem", psItem);
		modelMap.put("psVideoList", psVideoList);
		modelMap.put("pageNo", pageNo);
		modelMap.put("count", count);
		modelMap.put("pageCount", PageUtil.getPageNO(count, pageSize));
		modelMap.put("category", category);
		modelMap.put("code", code);
		return modelMap;
	}

	private PsVideoExample getPsVideoExampleBy(Page page, Integer category, String code,Integer psItemId) {
		PsVideoExample example = new PsVideoExample();
		example.setOrderByClause("zhangjie asc");
		if (page != null) {
			example.setStart(page.getBegin());
			example.setLimit(page.getLength());
		}
		Criteria criteria = example.createCriteria();
		if (category != null && category > 0) {
			criteria.andCategoryEqualTo(category);
		}
		if(StringUtil.isNotBlank(code)){
			criteria.andCodeEqualTo(code);
		}
		if (psItemId != null && psItemId >0 ) {
			criteria.andPsItemIdEqualTo(psItemId);
		}
		
		return example;

	}

}
